

$(document).ready(function () {
    init( callback, 1);
});


function init( callback, current) {
    $("#thislist").html("");
    $.ajax({
        type: "get",
        url: urlcore + "/api/find/selectAll?current=" + current ,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: {},
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            if (data.success == true) {
                callback(data.data.records);
                $('#thiscount').text(data.data.total);
                $("#pager").pager({
                    pagenumber: current,
                    pagecount: data.data.pages,
                    totalcount: data.data.total,
                    buttonClickCallback: PageClick
                });
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("查询失败！");
        }
    });

}


//回调函数
PageClick = function (pageclickednumber) {
    init( callback,1);
}

var callback = function (data) {
    var html = "";
    for (var i = 0; i < data.length; i++) {
        id = data[i].id;
        var picUrl = data[i].pictureUrl != null ? data[i].pictureUrl : "";
       // var addtime = data[i].addtime != null ? new Date(data[i].addtime).pattern("yyyy-MM-dd hh:mm:ss") : "";
        html += "  <tr id=\"" + id + "\">\n" +
            "  \t\t<td>" + (i + 1) + "</td>\n" +
            "  \t\t<td>" + data[i].title + "</td>\n" +
           // "  \t\t<td>" + picUrl + "</td>\n" +
            "\t\t <td class=\"footable-visible\"><img width=\"40px\" height=\"40px\" src=\"" + picUrl + "\"></td>\n" +
            "  \t\t<td>\n" +
            "   \t\t\t<a class=\"btn btn-danger btn-xs\" href=\"javascript:;\" onclick=\"doDelete(" + id + ")\"> 删除</a>\n" +
            "   \t\t\t<a class=\"btn btn-info btn-xs\"  href=\"javascript:;\" onclick=\"edit(" + id + ")\"> 修改</a>\n" +
            "  \t\t</td>\n" +
            "  \t</tr>";
    }
    $("#thislist").append(html);
}



function edit(id) {
    location.href = "find_add.html?id="+id;
}

//刪除
function doDelete(id) {
    if(confirm("确定删除吗？")) {
        $.ajax({
            type: "GET",
            url: urlcore + "/api/find/delete?id=" + id,
            dataType: "json",
            headers: {"Content-Type": "application/json;charset=UTF-8"},
            data: {},
            success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
                if (data.success == true) {
                    location.reload();
                } else {
                    alert(data.msg);
                }
            },
            error: function () {
                alert("删除失败");
            }
        });
    }
}


//搜索
function searchuser() {
    var type = $('#goods_type option:selected').val();//选中的值
    init(type, callback, 1);
}

/*
$(".pgNext").onclick(function () {
    init(type, callback, 1);
});
*/

